package com.example.hand.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.example.hand.bean.Order;
import com.example.hand.bean.PageBean;
import com.example.hand.dao.OrderDao;
import com.example.hand.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class OrderController{

    @Autowired
    OrderDao orderdao;//把OrderDao注入，就可以直接调用了(不用new对象。)

    @Autowired
    OrderService orderService;





    @RequestMapping("mainPage")//主页查询
    public String main(Model model, @RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage){
        PageBean<Order> pageBean = orderService.orderPage(currentPage);
        model.addAttribute("pageBean",pageBean);
        return "main";
    }

    @RequestMapping("delete")//删除
    public String delete(int order_id) {
        orderdao.deleteByPrimaryKey(order_id);
        return "redirect:mainPage";//重定向到主页面
    }


    @RequestMapping("adminAdd") //添加
    public String userAdd(Model model ) {
        model.addAttribute("order",new Order());
        return "adminAdd";
    }
    @RequestMapping("save")
    public  String save( Order order){
        int a =  orderService.add(order);
        System.out.println(order);
        return "redirect:mainPage";
    }

    @RequestMapping("adminEdit")//修改页
    public String userEdit(Model model,Integer order_id) {
        model.addAttribute("edit", orderdao.selecttest(order_id));
        System.out.print(66);
        return "adminEdit";
    }
    @RequestMapping("adminUpdate")//修改执行
    public String userupdate(String orderId,String cargo,String staffId,String procurementGoods,String address,String amount,String customerId,String orderDate,String expectedArrivalDate,String status,
                             HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model) {
        Order order=new Order();
        order.setAddress(address);//拿到前端传来的数据
        order.setCustomer_id(customerId);
        order.setCargo(cargo);
        order.setDelivery_date(expectedArrivalDate);
        order.setOrder_date(orderDate);
        order.setStaff_id(staffId);
        order.setStatus(status);
        order.setMoney(amount);
        order.setOrder_id(new Integer(orderId));
        int n = orderdao.updateByPrimaryKeySelective(order);//调用更新方法对数据库进行更行（执行更新的sql语句）
        List<Order> orderList = orderdao.selectByAll();//数据库更新之后把数据查询出来
        model.addAttribute("orderlist", orderList);
        return "redirect:mainPage";//重定向到main页面

    }

    //模糊查询，搜索框
    @RequestMapping(value = "selectorderByLike")
    public String selectorderByLike(HttpServletRequest request, HttpServletResponse response,
                                    HttpSession session, Model model, Order order,String content){
        if (content == "") {
            return "redirect:mainPage";
        }
        List<Order> sorderList = orderdao.queryOrderByLike(content);
        PageBean<Order>  pageBean = new PageBean<>();
        pageBean.setLists(sorderList);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("order",sorderList);//这里注意order要和html中的查询遍历那里的名字一样，否则查询框里没有数据。
        return "main";


    }

}
